import {Body, Controller, Get, Inject, Post, Req, UseGuards} from "@nestjs/common";
import {ApiOperation, ApiTags} from "@nestjs/swagger";
import {Result} from "../common/Result";
import {LoginDTO} from "./dto/LoginDTO";
import {AuthService} from "./AuthService";
import {JwtAuthGuard} from "./guard/JwtAuthGuard";
import {SkipAuth} from "./guard/GlobalGuardProvider";
import {JwtConstant} from "../common/constant/JwtConstant";

/**
 * 登录模块
 */
@ApiTags('登录')
@Controller('/auth')
export class AuthController {
    @Inject()
    private authService: AuthService;

    /**
     * 用户登录
     * @param user
     */
    @ApiOperation({summary: '登录'})
    @SkipAuth()
    @Post('/login')
    async login(@Body() user: LoginDTO): Promise<Result<any>> {
        return await this.authService.doLogin(user);
    }

    /**
     * 退出
     */
    @ApiOperation({summary: "退出"})
    // @SkipAuth()
    @Post("/logout")
    async logout () {
        let userId = JwtConstant.getUserId();
        if (!userId) {
            return Result.failed("用户ID不能为空!");
        }

        return Result.success("退出成功!");
    }

    /**
     * 测试JWT
     */
    @ApiOperation({summary: '测试JWT'})
    // @UseGuards(JwtAuthGuard)
    @Get('/profile')
    async profile() {
        return {
            msg: "成功",
            data: JwtConstant.getCurrentUser()
        };
    }

}
